﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Train_SelEmployees2 : System.Web.UI.Page
{
    private Hashtable ht = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //if (Srvtools.CliUtils.GetValue("_UserCode")[1] == null)
            //    Response.Redirect("~/InfoLogin.aspx?IsWorkFlow=true");
            
            string IssId = Request.Params["IssId"];
            string pIndex = Request.Params["pIndex"];
            this.Label1.Text = IssId;
            this.Label2.Text = pIndex;

            ht = this.GetEmpIdByIssId(IssId);

            this.InitTree();
        }
    }

    private void InitTree()
    {
        object[] myresult = Srvtools.CliUtils.CallMethod("Train", "GetEmpDepTrees", null);
        if (myresult != null && (int)myresult[0] == 0)
        {
            DataTable dt = (DataTable)myresult[1];

            DataView dv = dt.DefaultView;
            dv.RowFilter = "id=pid";

            for (int i = 0; i < dv.Count; i++)
            {
                TreeNode tn = new TreeNode(dv[i]["name"].ToString(), dv[i]["id"].ToString());
                tn.SelectAction = TreeNodeSelectAction.Expand;
                //tn.Expanded = false;
                this.TreeView1.Nodes.Add(tn);

                this.TreeBind(tn, dt);
            }

        }
    }

    private void TreeBind(TreeNode tn, DataTable dt)
    {
        DataView dv = new DataView(dt);
        dv.RowFilter = "pid='" + tn.Value + "' and pid<>id";

        for (int i = 0; i < dv.Count; i++)
        {
            TreeNode treenode = new TreeNode(dv[i]["name"].ToString(), dv[i]["id"].ToString());
            treenode.SelectAction = TreeNodeSelectAction.Expand;
            treenode.Expanded = false;
            tn.ChildNodes.Add(treenode);
            if (treenode.Value.StartsWith("e"))
            {
                string empId = treenode.Value.Remove(0, 1);
                treenode.Value = empId;
                treenode.ShowCheckBox = true;
                treenode.ImageUrl = "../Image/job.gif";

                if (ht != null)
                {
                    if (ht.Contains(empId))
                    {
                        treenode.Checked = true;
                        treenode.Value = treenode.Value + ",已选";
                        ListItem li = new ListItem(treenode.Text + "(" + treenode.Parent.Text + ")", treenode.Value);
                        this.lbEmps.Items.Add(li);
                    }
                    else
                    {
                        treenode.Value = treenode.Value + ",未选";
                    }
                }
            }
            else
            {
                treenode.ImageUrl = "../Image/dep.gif";
            }

            

            DataView temdv = new DataView(dt);
            temdv.RowFilter = "pid='" + treenode.Value + "' and pid<>id";
            if (temdv.Count > 0)
                TreeBind(treenode, dt);
        }
    }

    private Hashtable GetEmpIdByIssId(string IssId)
    {
        string empid = "";
        string empname = "";
        Hashtable ht = new Hashtable();
        object[] objs = new object[1];
        objs[0] = IssId;
        object[] result = Srvtools.CliUtils.CallMethod("Train", "GetEmpIdByIssId", objs);
        if (result != null && (int)result[0] == 0)
        {
            DataTable dt = (DataTable)result[1];
            Session["selected"] = dt;
            foreach (DataRow dr in dt.Rows)
            {
                empid = dr["EmployeeID"].ToString();
                empname = dr["EmpName"].ToString();

                ht.Add(empid, empname);
            }
        }
        return ht;
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        this.AddEmps(this.TreeView1.Nodes[0]);
    }

    private void AddEmps(TreeNode tn)
    {
        for (int i = 0; i < tn.ChildNodes.Count; i++)
        {
            if (tn.ChildNodes[i].Value.EndsWith("未选"))
            {
                if (tn.ChildNodes[i].Checked == true)
                {
                    ListItem li = new ListItem(tn.ChildNodes[i].Text + "(" + tn.Text + ")", tn.ChildNodes[i].Value);
                    this.lbEmps.Items.Add(li);

                }
               

            }
            if (tn.ChildNodes[i].ChildNodes.Count > 0)
                this.AddEmps(tn.ChildNodes[i]);
            
        }

        
        
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        DataTable selecteddt = (DataTable)Session["selected"];
        int max = 0;
        if (selecteddt.Compute("max(PId)", "") != DBNull.Value)
            max = Convert.ToInt32(selecteddt.Compute("max(PId)", ""));

        DataTable dt = new DataTable();
        dt.Columns.Add("PId");
        dt.Columns.Add("IssueId");
        dt.Columns.Add("EmployeeID");

        foreach (ListItem li in this.lbEmps.Items)
        {
            if (li.Value.Split(',').GetValue(1).ToString() == "未选")
            {
                DataRow dr = dt.NewRow();
                dr["PId"] = ++max;
                dr["IssueId"] = this.Label1.Text;
                dr["EmployeeID"] = li.Value.Split(',').GetValue(0).ToString();
                dt.Rows.Add(dr);
            }
        }

        if (dt.Rows.Count <= 0)
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('没有添加新的培训人员')", true);
            return;
        }

        object[] objs = new object[1];
        objs[0] = dt;
        object[] myresult = Srvtools.CliUtils.CallMethod("Train", "ExecTrain_Practice", objs);
        if (myresult != null && (int)myresult[0] == 0)
        {
            int temp = (int)myresult[1];

            if (temp > 0)
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "sucess(" + this.Label2.Text + ")", true);
            else
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "alert('添加人员失败！');", true);
        }
    }
    
}
